#include "game.h"


int x,y;

void Menu()
{
  printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
  printf("$$$$$$$$$  1.Play            0.Exit  $$$$$$$$$\n");
  printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
  printf("Please Select$ ");
}


//显示棋盘
void showboard(int board[][Col],int row,int col)
{
  printf("\e[1;1H\e[2J");
  int i=1,j=1;
  printf("  ");
  for(i=1;i<=col;i++){
    printf("%3d",i);
  }
  printf("\n");

  for(i=1;i<=row;i++){
    printf("%2d ",i);
    for(j=1;j<=col;j++){
      if(board[i-1][j-1] == 0){
        printf(" . ");
      }
      else if(board[i-1][j-1] == PLAYER1){
        printf(" ◉ ");
      }
      else {
        printf(" ◯ ");
      }
    }
    printf("\n");
  }
}

//按照以x,y作为起点，按照特定的方向，求连续相对最大棋子数
int ChressCount(int board[][Col],int row,int col,enum Dir d)
{
  int count = 0;
  int _x = x -1; //从0
  int _y = y -1; //从0
  while(1)
  {
    switch(d)
    {
      case Left:
        _y--;
        break;
      case Right:
        _y++;
        break;
      case Up:
        _x--;
        break;
      case Dowm:
        _x++;
        break;
      case Left_Up:
        _x--;_y--;
        break;
      case Left_Dowm:
        _x++;_y--;
        break;
      case Right_Up:
        _x--;_y++;
        break;
      case Right_Dowm:
        _x++;_y++;
        break;

      default:
        break;
    }
    if(_x < 0 || _x > row -1 || _y < 0 || _y > col -1)
    { //判断是否合法
      break;
    }
    //合法
    if(board[x-1][y-1] == board[_x][_y]){
      count++;
    }
    else{
      break;
    }
  }
  return count;
}


//4种情况
//NEXT：表示继续
//PLAYER1_WIN：用户1赢了
//PLAYER2_WIN：用户2赢了
//DRAW：平局
int IsOver(int board[][Col],int row,int col)
{ //判断五子连珠
  
  int count1 = 1 + ChressCount(board,row,col,Left) + ChressCount(board,row,col,Right); 
  int count2 = 1 + ChressCount(board,row,col,Up) + ChressCount(board,row,col,Dowm);
  int count3 = 1 + ChressCount(board,row,col,Left_Up) + ChressCount(board,row,col,Right_Dowm);
  int count4 = 1 + ChressCount(board,row,col,Right_Up) + ChressCount(board,row,col,Left_Dowm);
  
  if(count1 >= 5 || count2 >= 5 || count3 >= 5 || count4 >= 5){
    //有五子连珠
    //用户1 赢了
    if(board[x-1][y-1] == PLAYER1)
      return PLAYER1_WIN;
    //用户2 赢了
    if(board[x-1][y-1] == PLAYER2)
      return PLAYER2_WIN;
  }

  int i = 0;
  for(i=0;i<row;i++)
  {
    int j=0;
    for(j=0;j<col;j++)
    {
      if(board[i][j] == 0)
        return NEXT;
    }
  }

  return DRAW;
}


void PlayerMove(int board[][Col],int row,int col,int who)
{
  while(1){
    printf("Player[%d] Please Enter Your Pos$ ",who);
    scanf("%d %d",&x, &y);
    //确保输入的合法性
    if(x < 1 || x > row || y < 1 ||y > col){
      printf("Pos Is Not Right !\n");
      continue;
    }
    else if(board[x-1][y-1] != 0)
    {
      printf("Pos Is Occpuied !\n");
      continue;
    }
    else{
      board[x-1][y-1] = who;
      break;
    }
  }
}

void game()
{ 
  int board[Row][Col];
  memset(board,0,sizeof(board));
  int result = NEXT;
  do{

    showboard(board,Row,Col);
    PlayerMove(board,Row,Col,PLAYER1);
    result = IsOver(board,Row,Col);
    if(NEXT != result){
      break;//游戏结果：PLAYER1赢
    }

    showboard(board,Row,Col);
    PlayerMove(board,Row,Col,PLAYER2);
    result = IsOver(board,Row,Col);
    if(NEXT != result){
      break;//游戏结果 ：PLAYER2赢
    }
     
  }while(1);
  showboard(board,Row,Col);

  //p1 win, p2 win, draw
  switch(result)
  {
    case PLAYER1_WIN:
      printf("恭喜用户1，你已经赢了!\n");
      break;
    case PLAYER2_WIN:
      printf("恭喜用户2，你已经赢了!\n");
      break;
    case DRAW:
      printf("平局 !\n");
      break;

    default:
      break;
  }
}

